﻿using Inovout.Cloud.Applications.Models;
using Inovout.Cloud.Applications.Security.Models;
using Inovout.Cloud.Applications.Security.Services;
using Inovout.Cloud.Console.Web.Models;
using Inovout.Web.Mvc;
using System.Web.Mvc;

namespace Inovout.Cloud.Console.Web.Controllers
{
    public class WebApplicationCredentialController : CredentialController<WebApplicationCredential>
    {
        [HttpPost]
        public string UpdateClientSecret(string WebApplicationCredentialId)
        {
            WebApplicationCredential webApplicationCredential = base.RepositoryService.FindById(int.Parse(WebApplicationCredentialId));
            IWebApplicationCredentialService webappCredtialServer = (WebApplicationCredentialService)base.RepositoryService;
            webappCredtialServer.UpdateClientSecret(webApplicationCredential);
            if (base.Request.IsAjaxRequest())
            {
                return webApplicationCredential.ClientSecret.ToString();
            }
            else
            {
                return "";
            }
        }

        [HttpPost]
        public ActionResult Update(WebApplicationCredential WebApplicationCredential, string projectId)
        {
            IWebApplicationCredentialService webappCredtialServer = (WebApplicationCredentialService)base.RepositoryService;
            webappCredtialServer.Update(WebApplicationCredential);
            return RedirectToAction("My", "Credential", new { projectId = projectId });
        }

        public ActionResult Delete(int id, string projectId)
        {
            IWebApplicationCredentialService webappCredtialServer = (WebApplicationCredentialService)base.RepositoryService;
            webappCredtialServer.Delete(id);
            return RedirectToAction("My", "Credential", new { projectId = projectId });
        }

    }
}